<script setup>
import {register} from "@/api/user.js";
const $router = useRouter();

const registerForm = ref({
  username: 'hewu1122',
  phone: '15156053136',
  email: 'hewu1122@163.com',
  nickname: 'jack',
  password: 'admin888',
  password2:'admin888'
})

const validatePassword2 = (rule, value, callback) => {
  if (value !== registerForm.value.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
  ],
  nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  password2: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { validator: validatePassword2, trigger: 'blur' }
  ]
}

const registerFormRef = ref(null)
const doRegister = async () => {
  await registerFormRef.value.validate();
  register(registerForm.value).then(()=>{
    ElMessage.success('注册成功')
    $router.push('/login')
  })
}
</script>
<template>
  <div class="login-page">
    <el-card shadow="never" class="reg-form">
    <div class="title">智慧云智能教育平台-注册</div>
    <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="registerForm.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="registerForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="registerForm.nickname" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="registerForm.password" type="password" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password2">
        <el-input v-model="registerForm.password2" type="password" show-password placeholder="再次输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doRegister">立即注册</el-button>
        <el-button type="warning" @click="$router.push('/login')">返回登录</el-button>
      </el-form-item>
      <div class="flex justify-end">
        <router-link  class="link" to="/">返回主页</router-link>
      </div>
    </el-form>
  </el-card>
  </div>
</template>
<style scoped lang="scss">
.login-page{
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  background-image: url("@/assets/images/login-bg.png");
  background-size:100% 100%;
  background-position: center;
  position: fixed;
}
.reg-form{
  width: 600px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding-top: 20px;
  .title{
    text-align: center;
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 30px;
    color: var(--el-color-primary);
  }
  .link{
    cursor: pointer;
    color: var(--el-color-primary);
    font-size: 14px;
  }
}

</style>
